/**
 * 
 */

package de.unihannover.se.restlab.microblog.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.sun.jersey.api.NotFoundException;

import de.unihannover.se.restlab.microblog.data.UserStorage;
import de.unihannover.se.restlab.microblog.models.User;

public class UserResource {

	private User user;

	public UserResource (Integer id) {
		this.user = UserStorage.getStorage().getUser(id);
		if (this.user == null) throw new NotFoundException();
	}

	@GET
	@Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public Response show () {
		return Response.ok(user).build();
	}

	/* @GET
	 * 
	 * @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response getUser() { return
	 * Response.ok(user).build(); } */

	@PUT
	@Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response change (@FormParam("name") String name) {
		user.setName(name);
		return Response.ok(user).build();
	}

	@DELETE
	@Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public Response delete () {
		UserStorage.getStorage().removeUser(user);
		return Response.noContent().build();
	}

}
